﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Client.Filter
{
    public class LoginIn_Filter : IAuthorizationFilter
    {
        /// <summary>
        /// 动作过滤器
        /// </summary>
        /// <param name="filterContext"></param>
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            //获取控制器的名字
            string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;

            //获取Action的名字
            string actionName = filterContext.ActionDescriptor.ActionName;

            //判断当前请求的地址是不是满足过滤条件
            if (controllerName.ToLower() == "employee" && (actionName.ToLower() == "login" || actionName.ToLower() == "logindo"))
            {
                //满足条件继续执行不做任何操作
            }
            else
            {
                if (HttpContext.Current.Session["LoginInfo"] == null)
                {
                    //filterContext.Result = new RedirectResult("/Home/Login");

                    ContentResult res = new ContentResult();
                    res.Content = "<script>alert('请登录!');location.href='/Employee/Login';</script>'";
                    
                    filterContext.Result = res;
                }
            }
        }
    }
}